package com.zfh.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @title:
 * @author: fhzheng
 * @date: 3/16/2021 14:13
 * @description:
 */

@WebServlet("/jsonData")
public class JSONServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        resp.setContentType("text/html;charset=utf-8");

        JSONObject result = new JSONObject();
        JSONArray array = new JSONArray();

        for (int i = 0; i < 10; i++) {
            JSONObject temp = new JSONObject();
            temp.put("id",i+1000);
            temp.put("title","地点"+i+"号");
            array.add(temp);
        }
        result.put("code",200);
        result.put("msg","获取数据成功");
        result.put("status","OK");
        result.put("data",array);

        resp.getWriter().print(result);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doPost(req, resp);
    }
}
